/*
 * Copyright 2009 Gary Brown
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *
 * Change History:
 * 7 Mar 2009 : Initial version created by gary
 */
package org.betonthemove.ui.android;

import org.betonthemove.platform.BettingException;
import org.betonthemove.platform.BettingPlatform;
import org.betonthemove.platform.BettingPlatformFactory;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnDismissListener;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.*;

public class SignOn extends Activity implements OnClickListener,
					Runnable, OnCancelListener, OnDismissListener {
 
    private static final String LICENSE_ACCEPTED = "LicenseAccepted";
    private static final int LICENSE_DIALOG = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

        setContentView(R.layout.signon);
        
        m_username = (EditText)findViewById(R.id.signin_username);
        m_password = (EditText)findViewById(R.id.signin_password);
        m_status = (TextView)findViewById(R.id.signin_status);
        
        m_loginButton = (Button)findViewById(R.id.signin);
        m_loginButton.setOnClickListener(this);
		
		m_cancelButton = (Button)findViewById(R.id.cancelsignin);
		m_cancelButton.setOnClickListener(this);
		
        setProgressBarIndeterminateVisibility(false);

        SharedPreferences prefs=getPreferences(MODE_PRIVATE);

		// Check if license has been accepted - if not present it
		// to the user
		if (prefs == null || prefs.getBoolean(LICENSE_ACCEPTED,
							false) == false) {
			showDialog(LICENSE_DIALOG);
		}
    }

    public void onClick(View arg0) {
    	if (arg0 == m_cancelButton) {
    		finish();
    	} else if (arg0 == m_loginButton) {
    		
    		runOnUiThread(this);
    	}
    }

    public void run() {
    	
    	if (m_state == 0) {
    		setProgressBarIndeterminateVisibility(true);
    		
	        m_loginButton.setEnabled(false);
	        m_cancelButton.setEnabled(false);
	        m_username.setEnabled(false);
	        m_password.setEnabled(false);
	        
    		m_state++;
    		
    		new Thread(this).start();
    	} else if (m_state == 1) {

    		BettingPlatform platform=BettingPlatformFactory.getBettingPlatform();
        
	        try {
		        platform.login(m_username.getText().toString(),
		        		m_password.getText().toString());
		        
		        m_state++;
		        
		        runOnUiThread(this);
	        } catch(BettingException e) {
	        	
	        	m_state += 2;
	        	
	        	m_error = e.getLocalizedMessage();

		        runOnUiThread(this);
	        }
    	} else if (m_state == 2) {
    		
    		setProgressBarIndeterminateVisibility(false);
    		
            Intent intent = new Intent();
            intent.setClass(SignOn.this, EventTypeSelection.class);
            startActivity(intent);
            
            finish();

    	} else if (m_state == 3) {
    		
    		// Signed in failed
    		setProgressBarIndeterminateVisibility(false);
	        
	        m_username.setText("");
	        m_password.setText("");
	        
	        m_loginButton.setEnabled(true);
	        m_cancelButton.setEnabled(true);
	        m_username.setEnabled(true);
	        m_password.setEnabled(true);
	        
	        m_username.requestFocus();
	        
	        m_status.setText(m_error);
	        
    		m_state = 0;
    	}
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case LICENSE_DIALOG: {
                LicenseDialog dialog = new LicenseDialog(this);
                dialog.setOnCancelListener(this);
                dialog.setOnDismissListener(this);
                return dialog;
            }
        }
        return null;
    }

	public void onCancel(DialogInterface arg0) {
		finish();
	}

	public void onDismiss(DialogInterface arg0) {
		// Store preference to indicate license has been
		// accepted
		SharedPreferences prefs=getPreferences(MODE_PRIVATE);
		
		SharedPreferences.Editor editor=prefs.edit();
		editor.putBoolean(LICENSE_ACCEPTED, true);
		editor.commit();
	}
    
	private Button m_cancelButton=null;
    private Button m_loginButton=null;
    private EditText m_username=null;
    private EditText m_password=null;
    private TextView m_status=null;
    private int m_state=0;
    private String m_error=null;
}
